const Sequelize = require('sequelize');

import bcrypt from 'bcrypt'
import jwt from 'jsonwebtoken'
import config from '../../config'

import sequelize from './db';

//校验密码
function getUsers(queryOptions) {
    // const user = this;
    // const sql = 'select * from user limit 20';
    // user.findAll().spread(function(results, metadata) {
    //     console.log(results);
    // });
    // return new Promise((resolve, reject) => {
    //     bcrypt.compare(password, admin.password, (err, isMatch) => {
    //         if (err) {
    //             return reject(err)
    //         }

    //         resolve(isMatch)
    //     })
    // })
}

//生成Token
function updateUser(user) {
    // const user = this;
    // return jwt.sign({ id: admin.id }, config.token)
}

function deleteUser(uid) {

}
/**
 * 
 * uid          : 用户id
 * name         : 用户姓名
 * sex          : 用户性别 1男 2女
 * age          : 用户年龄
 * phone        : 用户手机号码
 * address      : 用户地址
 * status       : 1正常 0删除用户
 */
const User = sequelize.define('users', {
    uid: { type: Sequelize.STRING, primaryKey: true },
    name: { type: Sequelize.STRING, allowNull: false },
    sex: { type: Sequelize.INTEGER, allowNull: true },
    age: { type: Sequelize.INTEGER, allowNull: true },
    phone: { type: Sequelize.CHAR, allowNull: true },
    address: { type: Sequelize.STRING, allowNull: true },
    nickName: { type: Sequelize.STRING, allowNull: true },
    province: { type: Sequelize.STRING, allowNull: true },
    city: { type: Sequelize.STRING, allowNull: true },
    integral: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0 },
    status: { type: Sequelize.INTEGER, allowNull: true },
    icon: { type: Sequelize.STRING, allowNull: false },
    ctime: { type: Sequelize.INTEGER, allowNull: false }
}, {
    //实例方法
    instanceMethods: {
        getUsers: getUsers,
        updateUser: updateUser,
        deleteUser: deleteUser
    },

    //回调函数
    hooks: {
        beforeUpdate: function(admin) {
            // if (admin._changed.password || admin._changed.username) {
            //     const salt = bcrypt.genSaltSync(10);
            //     const p = bcrypt.hashSync(admin.password, salt);
            //     admin.password = p;
            // }
        },
        beforeCreate: function(admin) {
            const salt = bcrypt.genSaltSync(10);
            const p = bcrypt.hashSync(admin.password, salt);
            admin.password = p;
        }
    },
    charset: 'utf8'
});

    /**
    User.sync({force: false}).then(() => {});
    */

export default User;